<template>
  <el-dialog title="调账" :model-value="adjDialogVisible" width="360px" append-to-body>
    <div class="p-2">
      <el-card>
        <el-form ref="accountFormRef" :model="form" label-width="80px">
          <el-form-item label="变动类型" prop="bizType">
            <el-radio-group v-model="form.bizType" size="small">
              <el-radio key="increase" label="increase" border>增加</el-radio>
              <el-radio key="reduce" label="reduce" border>扣減</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="变动金额" prop="amount">
            <el-input-number v-model="form.amount" :step="10" />
          </el-form-item>
          <el-form-item label="谷歌验证" prop="validCode">
            <el-input v-model="form.validCode" placeholder="请输入谷歌验证码" style="width: 150px" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleChange">提交</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { adjugementAmount } from '@/api/retail/account';
import {propTypes} from "@/utils/propTypes";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const props = defineProps({
  // 賬號ID
  adjDialogVisible: propTypes.bool.def(false),
  id: propTypes.string.def("0"),
  accountName: propTypes.string.def("")
});
const emit = defineEmits(['update:adjDialogVisible']);
const form = ref<any>({ bizType: "increase", amount: "", validCode:"" });
const handleChange = async () => {
  if(!form.value.validCode) {
    proxy?.$modal.msgWarning("谷歌验证码空");
    return;
  }
  let title = form.value.bizType=='increase'?"增加":"扣减";
  await proxy?.$modal.confirm("是否确认对账户("+props.accountName+")"+title+"金额："+form.value.amount+"元");
  const res = await adjugementAmount(props.id, form.value.bizType, form.value.amount, form.value.validCode).finally(()=>{
    emit('update:adjDialogVisible', false);
  });
  if(res.code==200) {
    proxy?.$modal.msgSuccess("調賬成功");
  }else {
    proxy?.$modal.msgError("調賬失敗");
  }
}
</script>
